package com.brkj.course;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.*;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface.HIF_DeleteFavoriteCourse;
import com.brkj.httpInterface.HttpInterface.HIF_GetFavoriteCourse;
import com.brkj.main.R;
import com.brkj.model.DS_Course;
import com.brkj.util.ImgShow;
import com.brkj.util.JsonUtil;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.PullListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class CourseViewFavarite {

    private PullListView courseListView;
    private TextView noContent;

    private View view;
    private Context mContext;

    private boolean hasShown = false;
    private List<DS_Course> courseList = new ArrayList<DS_Course>();

//	@Override
//	protected void onCreate(Bundle savedInstanceState) {
//		setAnimType(BaseActivity.RIGHT_ENTER_LEFT_EXT);
//		super.onCreate(savedInstanceState);
//
//		setContentView(R.layout.favorite_course);
//		setReturnBtn();
//		setActivityTitle("收藏");
//
//		getFavoriteCourse();
//	}

    public CourseViewFavarite(Context context){
        mContext = context;
    }

    public View getView(){
        view =  LayoutInflater.from(mContext).inflate(R.layout.favorite_course, null);

        courseListView = (PullListView)view.findViewById(R.id.courseListView);
        courseListView.setonRefreshListener(new PullListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getFavoriteCourse();
            }
        });

        courseListView.hideFooterView();

        noContent = (TextView)view.findViewById(R.id.noContent);
        return view;
    }

    public void show(){
        if((hasShown)&& (courseList.size()>0)){
            return;
        }else{
            getFavoriteCourse();
            hasShown = true;
        }
    }


	private void getFavoriteCourse() {
		BaseAjaxParams params = new BaseAjaxParams();
		params.put(HIF_GetFavoriteCourse.params.UserID, MyApplication.myUserID);
		
		FinalHttp fh = new FinalHttp();
		fh.post(HIF_GetFavoriteCourse.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)mContext) {

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				
				Gson gson = new Gson();
				Type listType = new TypeToken<List<DS_Course>>(){}.getType();
				courseList = gson.fromJson((String)t, listType);
				
				if((courseList != null) && (courseList.size()>0)){
					CourseFavoriteAdapter adapter = new CourseFavoriteAdapter(mContext, courseList);
					
					courseListView.setAdapter(adapter);
				}else{
					courseListView.setVisibility(View.GONE);
					noContent.setVisibility(View.VISIBLE);
				}

                courseListView.onRefreshComplete();
			}
			
			
		});
		
	}

	
	
	public class CourseFavoriteAdapter extends BaseAdapter{
		private ListItemView listItemView;
		protected LayoutInflater listContainer; 
		private Context context;
		private List<DS_Course> courseList;
		private FinalBitmap fb;
		
		public CourseFavoriteAdapter(Context context, List<DS_Course> courseList){
			this.context = context;
			this.courseList = courseList;
			
			listContainer = LayoutInflater.from(context);
			fb = FinalBitmap.create(context);
		}
		
		private  class ListItemView{
			public ImageView CourseImg;
			public TextView Title;
			public TextView Content;
			public TextView Time;
			public TextView StudyCount;
			public Button delete;
			public TextView teacher;
			public TextView course_time;
		};
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return courseList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public void remove(int position) {
			courseList.remove(position);
			notifyDataSetChanged();
			
			if(getCount() == 0){
				noContent.setVisibility(View.VISIBLE);
			}
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			// 自定义视图
			listItemView = new ListItemView();
			// 获取list_item布局文件的视图
			if (convertView == null) {
				convertView = listContainer.inflate(R.layout.course_downloaded_item, null);
				// 获取控件对象
				listItemView.CourseImg = (ImageView) convertView.findViewById(R.id.CourseImg);
				listItemView.Title = (TextView) convertView
						.findViewById(R.id.Title);
				listItemView.Content = (TextView) convertView
						.findViewById(R.id.Content);
				listItemView.Time = (TextView) convertView
						.findViewById(R.id.Time);
				listItemView.StudyCount = (TextView) convertView
						.findViewById(R.id.StudyCount);
				listItemView.teacher = (TextView) convertView
						.findViewById(R.id.teacher);
				listItemView.course_time = (TextView) convertView
						.findViewById(R.id.course_time);
				
				listItemView.delete = (Button) convertView
						.findViewById(R.id.delete);

				// 设置控件集到convertView
				convertView.setTag(listItemView);
			} else {
				listItemView = (ListItemView) convertView.getTag();
			}
			
//			fb.display(listItemView.CourseImg, courseList.get(position).getCourseImg());
			ImgShow.display(listItemView.CourseImg, courseList.get(position).getCourseImg());
			
//			ImageLoader.getInstance().displayImage(
//					"http://118.192.9.79:7002/KMELearning//WebFiles/cwFiles/52266A266-BBFF-4482-A6A7-02196AB18763k.jpg"
//					, listItemView.CourseImg);
			listItemView.Title.setText(courseList.get(position).getTitle());
			listItemView.Content.setText(courseList.get(position).getContent());
			listItemView.Time.setText("课程时长：" + courseList.get(position).getTime());
			listItemView.StudyCount.setText(
					"学习(" + courseList.get(position).getStudyCount()+ ")");
			listItemView.teacher.setText("讲师姓名：" + courseList.get(position).getTeacher());
			listItemView.course_time.setText(courseList.get(position).getSubmitTime());
			
			listItemView.delete.setVisibility(View.VISIBLE);
			
			listItemView.delete.setTag(R.id.tag_first, courseList.get(position));
			listItemView.delete.setTag(R.id.tag_second, position);
			listItemView.delete.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					deleteCourse((DS_Course)arg0.getTag(R.id.tag_first));
					deleteFavorityCourse((DS_Course)arg0.getTag(R.id.tag_first));
					remove((Integer)arg0.getTag(R.id.tag_second));
				}

			});
			
			convertView.setOnClickListener(new MyClickListener(position));
			
			return convertView;
			
		}

		protected void deleteFavorityCourse(DS_Course course) {
			BaseAjaxParams params = new BaseAjaxParams();
			params.put(HIF_DeleteFavoriteCourse.params.CourseID, Integer.toString(course.getCourseID()));
			params.put(HIF_DeleteFavoriteCourse.params.UserID, MyApplication.myUserID);
			
			FinalHttp fh = new FinalHttp();
			fh.post(HIF_DeleteFavoriteCourse.address, params, new AjaxCallBack<Object>() {

				@Override
				public void onSuccess(Object t) {
					// TODO Auto-generated method stub
					super.onSuccess(t);
					HIF_DeleteFavoriteCourse.result result = 
							JsonUtil.fromJson((String)t, HIF_DeleteFavoriteCourse.result.class);
					
					if(result != null){
						if(result.result == 1){
							//取消收藏成功
						}
					}
					
				}
				
				
			});
		}

		protected void deleteCourse(DS_Course course) {
			BaseAjaxParams params = new BaseAjaxParams();
			params.put(HIF_DeleteFavoriteCourse.params.CourseID, Integer.toString(course.getCourseID()));
			params.put(HIF_DeleteFavoriteCourse.params.UserID, MyApplication.myUserID);
			
			FinalHttp fh = new FinalHttp();
			fh.post(HIF_DeleteFavoriteCourse.address, params, new AjaxCallBack<Object>() {

				@Override
				public void onSuccess(Object t) {
					super.onSuccess(t);
					
					HIF_DeleteFavoriteCourse.result result = 
							JsonUtil.fromJson((String)t, HIF_DeleteFavoriteCourse.result.class);
					
					
				}
			});
		}
		
		
		class MyClickListener implements OnClickListener{
			private int position;
			
			public MyClickListener(int position){
				this.position = position;
			}
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(context, CourseDetailActivity.class);
				DS_Course course = new DS_Course();
				
				intent.putExtra("course", courseList.get(position));
				context.startActivity(intent);
			}
			
		}
	}


}
